package com.kla.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.entity.AreaEntity;
import com.kla.power.mapper.AreaMapper;
import com.kla.power.service.IAreaService;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 省市县区 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2023-06-23
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaEntity> implements IAreaService {

    @Override
    public List<AreaEntity> queryList ( AreaEntity areaEntity ) {
        if ( areaEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        QueryWrapper<AreaEntity> queryWrapper = new QueryWrapper<> ();
        if ( StringUtils.isNotBlank ( areaEntity.getAreaName () ) ) {
            queryWrapper.like ( "areaName", areaEntity.getAreaName () );
        }
        if ( StringUtils.isNotBlank ( areaEntity.getParentId () ) ) {
            queryWrapper.eq ( "parentId", areaEntity.getParentId () );
        }
        List<AreaEntity> areaEntities = baseMapper.selectList ( queryWrapper );
        return areaEntities;
    }

    @Override
    public AreaEntity saveSingle ( AreaEntity areaEntity ) {
        if ( areaEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( areaEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        areaEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( areaEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return areaEntity;
    }

    @Override
    public AreaEntity querySingle ( AreaEntity areaEntity ) {
        if ( areaEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( areaEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        AreaEntity area = baseMapper.selectById ( areaEntity );
        return area;
    }
}
